package com.classroom.smart.controller;

import com.classroom.smart.pojo.Complain;
import com.classroom.smart.result.CommonResult;
import com.classroom.smart.result.Result;
import com.classroom.smart.service.ComplainService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @Auther:YouGuoyun
 * @Title:leetcode
 * @Date:Create in 2023/2/22 18:39
 **/

@RestController
@RequestMapping("/complain")
public class ComplainController {

    @Autowired(required = false)
    private ComplainService complainService;

    @PostMapping("/submit")
    public Result submit(@RequestBody Complain complain){
        System.out.println("执行插入" + complain);
        complainService.insert(complain);
        System.out.println("插入成功");
        Result result = new Result();
        result.setFlag(true);
        return result;

    }

    //查找所有未处理投诉
    @GetMapping("viewComplainList")
    @ResponseBody
    public CommonResult viewComplain(
            @RequestParam(value = "current",required = false,defaultValue = "1")int current,
            @RequestParam(value = "complainStatus",required = false)int complainStatus
    ){
        List<Complain> complains = complainService.viewAllNotProcess(current,complainStatus);
        CommonResult commonResult = null;
        if (complains != null && complains.size() != 0){
            PageInfo<Complain> pageInfo = new PageInfo<>(complains);
            HashMap<String,Object> hashMap = new HashMap<>();
            hashMap.put("pageInfo",pageInfo);
            hashMap.put("complainStatus",complainStatus);
            commonResult = new CommonResult(200,"查询成功",hashMap);
        }else {
            commonResult = new CommonResult(455,"查询失败");
        }
        return commonResult;
    }


    //删除
    @DeleteMapping("deleteComplain/{id}")
    @ResponseBody
    public CommonResult delete(@PathVariable int id){
        boolean flag = complainService.deleteComplain(id);
        CommonResult commonResult = null;
        if (flag){
            commonResult = new CommonResult(200,"删除成功",flag);
        }else {
            commonResult = new CommonResult(455,"删除失败");
        }
        return commonResult;
    }

    //获取要更新的信息
    @GetMapping("getComplain/{id}")
    @ResponseBody
    public CommonResult getUpteComplain(@PathVariable int id){
        CommonResult commonResult = null;
        Complain complain = complainService.getUpdateComplain(id);
        if (complain != null){
            commonResult = new CommonResult(200,"查询成功",complain);
        }else {
            commonResult = new CommonResult(499,"查询失败");
        }
        return commonResult;
    }

    //更新
    @PutMapping("updateComplain")
    @ResponseBody
    public CommonResult updeteComplain(@RequestBody Complain complain){
        CommonResult commonResult = null;
        boolean flag = complainService.updateComplain(complain);
        if (flag){
            commonResult = new CommonResult(200,"跟新成功",flag);
        }else {
            commonResult = new CommonResult(499,"更新失败");
        }
        return commonResult;
    }

    //搜索
    @GetMapping("searchComplain")
    @ResponseBody
    public CommonResult selectComplain(
            @RequestParam(value = "current" , required = false , defaultValue = "1") int current ,
            @RequestParam(value = "query" , required = false , defaultValue = "") String query,
            @RequestParam(value = "complainStatus",required = false,defaultValue = "")int complainStatus
    ){
        //查询用户列表
        List<Complain> complains = complainService.selectCompains(current, query,complainStatus);
        CommonResult commonResult = null;
        if(complains!=null && complains.size()!=0){
            //封装PageInfo对象
            PageInfo<Complain> pageInfo = new PageInfo<>(complains);
            //将PageInfo、查询条件一起封装至Map集合中
            HashMap<String,Object> map = new HashMap<>();
            map.put("pageInfo",pageInfo);
            map.put("query",query);
            map.put("complainStatus",complainStatus);
            commonResult = new CommonResult(200 , "查询成功" , map);
        }else{
            commonResult = new CommonResult(477 , "查询失败");
        }
        return commonResult;
    }

}
